//
//  MYGChatEnterView.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/1.
//

import UIKit

class MYGChatEnterView: UIView {
    // MARK: - Property
    var emojiAction: (() -> Void)?
    var addAction: (() -> Void)?
    
    var isFire: Bool = false {
        didSet {
            fileBtn1?.isSelected = isFire
            fileBtn2?.isSelected = isFire
        }
    }

    var recoardAction: ((Bool) -> Void)?
    
    private var fileBtn1: UIButton?
    private var fileBtn2: UIButton?
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        addSubview(stackView)
        
        stackView.addArrangedSubviews([voiceButton, textField, sayButton, emojiButton, addButton])
        
        voiceButton.setContentHuggingPriority(.required, for: .horizontal)
        voiceButton.setContentCompressionResistancePriority(.required, for: .horizontal)
        emojiButton.setContentHuggingPriority(.required, for: .horizontal)
        emojiButton.setContentCompressionResistancePriority(.required, for: .horizontal)
        addButton.setContentHuggingPriority(.required, for: .horizontal)
        addButton.setContentCompressionResistancePriority(.required, for: .horizontal)
        
        textField.snp.makeConstraints { make in
            make.height.equalTo(31)
        }
        
        sayButton.snp.makeConstraints { make in
            make.height.equalTo(31)
        }
        
        stackView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(11)
            make.top.bottom.equalToSuperview()
        }
    }
    // MARK: - lazy load
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .horizontal
        stackV.spacing = 9
        stackV.alignment = .center
        return stackV
    }()
    private lazy var voiceButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Chat/Enter/voice"), for: .normal)
        btn.setImage(UIImage(named: "Chat/Enter/keyboard"), for: .selected)
        
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private(set) lazy var textField: UITextField = {
        let tf = UITextField()
        tf.backgroundColor = .rgba(248, 248, 249, 1)
        tf.font = .regular(14)
        tf.textColor = .rgba(51, 51, 51, 1)
        
        tf.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 17, height: 31))
        tf.leftViewMode = .always
        tf.returnKeyType = .send
        
        tf.attributedPlaceholder = NSMutableAttributedString.rz.colorfulConfer(confer: { confer in
            confer.text("输入你的信息内容")?.font(.regular(14)).textColor(.rgba(102, 102, 102, 1))
        })
        
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 45, height: 31))
        let button = UIButton()
        self.fileBtn1 = button
        button.setImage(UIImage(named: "Chat/fire"), for: .normal)
        button.setImage(UIImage(named: "Chat/fire_sel"), for: .selected)
        button.isSelected = isFire
        button.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        button.sizeToFit()
        view.addSubview(button)
        button.center = view.center
        tf.rightViewMode = .always
        tf.rightView = view
        
        tf.layer.masksToBounds = true
        tf.layer.cornerRadius = 15
        tf.layer.borderWidth = 0.5
        tf.layer.borderColor = UIColor.rgba(235, 235, 235, 1).cgColor
        return tf
    }()
    
    private lazy var sayButton: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = .rgba(248, 248, 249, 1)
        btn.setTitle("按住 说话", for: .normal)
        btn.setTitleColor(.rgba(102, 102, 102, 1), for: .normal)
        btn.titleLabel?.font = .regular(14)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 15
        btn.layer.borderWidth = 0.5
        btn.layer.borderColor = UIColor.rgba(235, 235, 235, 1).cgColor
        
        let button = UIButton()
        self.fileBtn2 = button
        button.setImage(UIImage(named: "Chat/fire"), for: .normal)
        button.setImage(UIImage(named: "Chat/fire_sel"), for: .selected)
        button.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        button.isSelected = isFire
        btn.addSubview(button)
        
        button.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(13)
            make.centerY.equalToSuperview()
        }
        
        btn.isHidden = true
        
        let press = UILongPressGestureRecognizer(target: self, action: #selector(pressVoice))
        btn.addGestureRecognizer(press)
        return btn
    }()
    
    private lazy var emojiButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Chat/Enter/emoji"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
    
    private lazy var addButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Chat/Enter/add"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
}

extension MYGChatEnterView {
    @objc
    private func clickButton(_ btn: UIButton) {
        if btn == voiceButton {
            textField.resignFirstResponder()
            btn.isSelected = !btn.isSelected
            textField.isHidden = btn.isSelected
            sayButton.isHidden = !btn.isSelected
        } else if (btn == emojiButton) {
            textField.resignFirstResponder()
            emojiAction?()
        } else if (btn == addButton) {
            textField.resignFirstResponder()
            addAction?()
        } else {
            btn.isSelected = !isFire
            isFire = !isFire
        }
    }
    
    @objc
    private func pressVoice(_ press: UILongPressGestureRecognizer) {
        switch press.state {
        case .began:
            recoardAction?(true)
        case .ended:
            recoardAction?(false)
        default:
            break
        }
    }
}
